import app from '@/common/config/app.js';
import {
	getRouteUrl,
	getPage
} from '@/common/utils/router.js';
import {
	eventBus
} from '@/common/utils/eventBus.js';

const dialog = {
	alert(params) {
		let newParams = {},
			content;
		if (typeof params === 'string') {
			content = params;
		} else {
			content = params.content;
			newParams = {
				...params
			}
		}
		return new Promise((resolve, reject) => {
			if (app.nativeAlert || newParams.nativeAlert) {
				uni.showModal({
					title: '提示',
					content: content,
					showCancel: false,
					...newParams,
					success: function(res) {
						if (res.confirm) {
							resolve()
						} else if (res.cancel) {
							reject()
						}
					}
				});

			} else {
				const query = uni.createSelectorQuery();
				const nodesRef = query.select('.cg-custom-dialog').boundingClientRect(rect => {
					if (rect) {
						uni.$emit(`customAlert${getRouteUrl()}`, getRouteUrl(), {
							title: '提示',
							content: content || params.message,
							showCancel: false,
							...newParams,
							success: () => {
								resolve()
							},
							cancel: () => {
								reject()
							}
						});
					} else {
						console.warn(
							"cg-custom-alert component is necessary on custom mode!please use in current page, otherwise you won't see the popup dialog"
						)
					}
				}).exec();
			}

		})
	},
	confirm(params) {
		let newParams = {};
		let content;
		if (typeof params === 'string') {
			content = params;
		} else {
			newParams = {
				...params
			}
		}
		return new Promise((resolve, reject) => {
			if (app.nativeAlert || newParams.nativeAlert) {

				uni.showModal({
					title: '提示',
					content: content || params.message,
					showCancel: true,
					...newParams,
					success: function(res) {
						if (res.confirm) {
							resolve()

						} else if (res.cancel) {
							reject()
						}
					}
				});
			} else {

				const query = uni.createSelectorQuery();
				query.select('.cg-custom-dialog').boundingClientRect(rect => {
					if (rect) {
						console.log('eventBus.list():', eventBus.list());
						uni.$emit(`customAlert${getRouteUrl()}`, getRouteUrl(), {
							title: '提示',
							content: content || params.message,
							showCancel: true,
							...newParams,
							success: () => {
								resolve()
							},
							cancel: () => {
								reject()
							}

						});
					} else {
						console.warn(
							"cg-custom-alert component is necessary on custom mode!please use in current page, otherwise you won't see the popup dialog"
						)
					}
				}).exec();

			}

		})
	},
	loading(params) {
		let newParams = {};
		let content = 'loading...';
		if (typeof params === 'string') {
			content = params;
		} else {
			newParams = {
				...params
			}
		}
		return new Promise((resolve, reject) => {
			if (app.nativeAlert || newParams.nativeAlert) {
				if (newParams.duration) {
					setTimeout(() => {
						uni.showLoading({
							title: content,
							mask: true,
							...newParams,
							success: function(res) {
								if (res.confirm) {
									resolve()

								} else if (res.cancel) {
									reject()
								}
							}
						});
					}, newParams.duration)
				} else {

					uni.showLoading({
						title: content,
						mask: true,
						...newParams,
						success: function(res) {
							if (res.confirm) {
								resolve()

							} else if (res.cancel) {
								reject()
							}
						}
					});
				}


			} else {
				const query = uni.createSelectorQuery();
				query.select('.cg-custom-loading').boundingClientRect(rect => {
					if (rect) {
						uni.$emit(`openCustomLoading${getRouteUrl()}`, getRouteUrl(), {
							message: 'loading',
							...newParams,
							success: () => {
								resolve()
							},
							cancel: () => {
								reject()
							}

						});
					} else {
						console.warn(
							"cg-custom-loading component is necessary on custom mode!please use in current page, otherwise you won't see the popup loading"
						)
					}
				}).exec();
			}


		})
	},
	hideLoading(config) {
		let newParams;
		let timeout = 0;

		let params = config || {};
		if (typeof params === 'number') {
			newParams = {
				timeout: params
			}

		} else {
			newParams = {
				...params
			}
		}
		if (app.nativeAlert || newParams.nativeAlert) {
			setTimeout(() => {
				uni.hideLoading()
			}, newParams.timeout)

		} else {
			const query = uni.createSelectorQuery();
			query.select('.cg-custom-loading').boundingClientRect(rect => {
				if (rect) {
					uni.$emit(`closeCustomLoading${getRouteUrl()}`, getRouteUrl(), {
						timeout: timeout,
					});
				} else {
					console.warn(
						"cg-custom-loading component is necessary on custom mode!please use in current page, otherwise you won't see the popup loading"
					)
				}
			}).exec();
		}

	},
	toast(params) {
		let newParams, content;
		if (typeof params === 'string') {
			content = params;
		} else {
			content = params.message;
			newParams = {
				...params
			}
		}
		return new Promise((resolve, reject) => {
			uni.showToast({
				mask: true,
				icon: 'none',
				duration: 1500,
				title: content || '提示',
				showCancel: false,
				...newParams,
				success: function(res) {
					if (res.confirm) {
						resolve()

					} else if (res.cancel) {
						reject()
					}
				}
			});

		})
	},
}
export default dialog